//
//  H5WebViewController.m
//  JieJieKan
//
//  Created by 极速现金侠 on 2017/6/19.
//  Copyright © 2017年 innext. All rights reserved.
//

#import "H5WebViewController.h"
#import "H5WebView.h"
@interface H5WebViewController ()<H5WebViewDelegate>
@property (nonatomic , weak) H5WebView *webView;
@end

@implementation H5WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self baseConfigBackTitle:PageGobackTypePop];
    
//    self.navigationItem.title = self.titles;
    
    H5WebView *webView = [[H5WebView alloc] initWithFrame:self.view.bounds];
    NSString *strURL= self.url;
    NSURL *url =[NSURL URLWithString:strURL];
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    webView.webViewDelegate = self;
    [self.view addSubview:webView];
    self.webView = webView;
    
    [self.webView addObserver:self
                   forKeyPath:@"loading"
                      options:NSKeyValueObservingOptionNew
                      context:nil];
    
    [self.webView addObserver:self
                   forKeyPath:@"title"
                      options:NSKeyValueObservingOptionNew
                      context:nil];
    
    [self.webView addObserver:self
                   forKeyPath:@"estimatedProgress"
                      options:NSKeyValueObservingOptionNew
                      context:nil];
    
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.equalTo(self.view);
        make.width.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
}


- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary<NSString *,id> *)change
                       context:(void *)context
{
    
    
    if ([keyPath isEqualToString:@"loading"]){
        NSLog(@"loading");
        
    }else if ([keyPath isEqualToString:@"title"]){
        
        
    }else if ([keyPath isEqualToString:@"estimatedProgress"]){
        
        NSLog(@"progress: %f", self.webView.estimatedProgress);
        self.webView.progressView.progress = self.webView.estimatedProgress;
    }
    
    // 加载完成
    if (!self.webView.loading)
    {
        
        [UIView animateWithDuration:0.5 animations:^{
            self.webView.progressView.alpha = 0.0f;
        } completion:^(BOOL finished) {
            [self.webView.progressView removeFromSuperview];
            self.webView.progressView = nil;
        }];
    }
}


#pragma mark - H5WebViewDelegate
- (void)newsDetialWebView:(H5WebView *)webView title:(NSString *)title{
//    self.navigationItem.title = title;
    
}

- (void)persentViewController:(UIAlertController *)alertController{
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc{
    [self.webView removeObserver:self forKeyPath:@"loading"];//移除kvo
    [self.webView removeObserver:self forKeyPath:@"title"];
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    self.webView.scrollView.delegate = nil;
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
